import os.path
from time import sleep

from selenium import webdriver
from selenium.common import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import json
import logging
from selenium.webdriver.chrome.options import Options

import conf

"""
获取抖音创作者平台cookie
"""

def get_douyin_cookies():
    global cookies
    chrom_options=Options()

    # chrom_options.add_argument('--headless')
    driver=webdriver.Chrome(options=chrom_options)
    driver.get('https://creator.douyin.com/')

    try:
        element = WebDriverWait(driver, 50).until(
            EC.presence_of_element_located((By.ID, 'douyin-creator-master-side-upload')))
        if element:

            cookies = driver.get_cookies()
            cookies_file = conf.douyin_upload_cookie_flil_path
            with open('cookies_file', 'w') as file:
                json.dump(cookies, file, indent=4)
            logging.info('cookie有效,已更新')
            sleep(100)
            return cookies

    except TimeoutException:
        print("无法在指定的时间内找到元素")
    finally:
        driver.quit()



print(get_douyin_cookies())

